<?php
/**
 * Joomla! 1.5 component jhtraq
 *
 * @version $Id: view.html.php 2010-03-09 08:28:06 svn $
 * @author JHShop
 * @package Joomla
 * @subpackage jhtraq
 * @license GNU/GPL
 *
 * Traq type system for Joomla
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

function sortNames($a, $b)
{ 
	$retval = strnatcmp($a['type'], $b['type']);
	if(!$retval) 
	    return strnatcmp($a['path'], $b['path']);
	return $retval;
} 
	
/**
 * HTML View class for the jhtraq component
 */
class JhtraqViewSvn extends JView {
	function display($tpl = null) {
		$model =& JhtraqHelper::getModel('svn');
		$project = JRequest::getVar('project');
		//echo $project;
		$this->project = $model->getProjectBySlug($project);
		
		$userGroup = $model->GetMyGroup();
		
		if(in_array($userGroup['group_id'], explode(",", $this->project->browse_svn)) || in_array($userGroup['group_id'], explode(",", $this->project->svn_logs)))
		{
			if($this->project->svn_url != '')
			{
				require_once(JPATH_COMPONENT . DS . 'extras'.DS.'svn'.DS.'phpsvnclient.php');
				$this->svn = new phpsvnclient($this->project->svn_url, $this->project->svn_username, $this->project->svn_password);
				
				$this->svnCache = new svnCache;
				$version = $this->svnCache->versionCheck($project);
				if(!$version)
				{
					$version = $this->svn->getVersion();
					$this->svnCache->cache('svnGetVersion'.$project,$version);
				}
				//print_r($this->svn);
				//die($version);
				$this->version = $version;
				
				if(in_array($userGroup['group_id'], explode(",", $this->project->browse_svn)))
				{
					$defaultAction = 'browse_svn';
				}else{
					$defaultAction = 'svn_logs';
				}
				
				$this->project->display = $this->project->browse_svn;
				$this->project->diff = $this->project->browse_svn;
				$this->project->viewlog = $this->project->svn_logs;
				
				$action = JRequest::getVar('action', $defaultAction);
				
				if(in_array($userGroup['group_id'], explode(",", $this->project->$action)))
				{
					$path = JRequest::getVar('path','/');
					$this->path = $path;
					//die($path);
					switch($action)
					{
						case "browse_svn":
							$this->view_logs = false;
							if(in_array($userGroup['group_id'], explode(",", $this->project->svn_logs)))
							{
								$this->view_logs = true;
							}
							
							$this->files = $this->svnCache->fetch('svnGetDirectoryFiles'.$project.$path.$version, true);
							if(!$this->files)
							{
								$this->files = $this->svn->getDirectoryFiles($path);
								$this->svnCache->cache('svnGetDirectoryFiles'.$project.$path.$version,$this->files);
							}
							usort($this->files, 'sortNames');
							break;
							
						case "svn_logs":
							$logs = $this->svnCache->fetch('svnGetLogFile'.$project.$version,true);
							if(!$logs)
							{
								$logs = $this->svn->getRepositoryLogs(0,-1);
								$this->svnCache->cache('svnGetLogFile'.$project.$version,$logs);
								
							}
							$tpl = 'log';
							krsort($logs);
							
							//Pagination
							jimport('joomla.html.pagination');
							$mainframe =& JFactory::getApplication();
							$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
							$limitstart = $mainframe->getUserStateFromRequest( 'jhtraqlimitstart', 'limitstart', 0, 'int' );
							$this->_pagination = new JPagination( count($logs), $limitstart, $limit );
							$this->logs = array_slice($logs,$limitstart,$limit);
							
							
							break;
							
						case "display":
							$this->file = $this->svnCache->fetch('svnDisplayFile'.$project.$path.$version);
							if(!$this->file)
							{
								$this->file = $this->svn->getFile($path);
								$this->svnCache->cache('svnDisplayFile'.$project.$path.$version,$this->file);
								
							}
							$tpl = 'display';
							$this->path = $path;
							//htmlentities($svn->getFile($path));
							break;
							
						case "viewlog":
							$v = JRequest::getVar('v',0);
							$this->v = $v;
							if(in_array($userGroup['group_id'], explode(",", $this->project->browse_svn)))
							$this->canview = true;
							//die($v);
							$v--;
							$this->logs = $this->svnCache->fetch('svnGetLogFileView'.$project.$v,true);
							if(!$this->logs)
							{
								$this->logs = $this->svn->getRepositoryLogs($v,$v+1);
								$this->svnCache->cache('svnGetLogFileView'.$project.$v,$this->logs);
								
							}
							$tpl = 'logview';
							break;
						case "diff":
							$current = JRequest::getVar('version',$version);
							$previos = $current -1;
							$this->previos = $previos;
							$this->current = $current;
							$this->file = $this->svnCache->fetch('svnDisplayFile'.$project.$path.$current);
							if(!$this->file)
							{
								$this->file = $this->svn->getFile($path,$current);
								$this->svnCache->cache('svnDisplayFile'.$project.$path.$current,$this->file);
								
							}
							$this->oldfile = $this->svnCache->fetch('svnDisplayFile'.$project.$path.$previos);
							if(!$this->oldfile)
							{
								$this->oldfile = $this->svn->getFile($path,$previos);
								$this->svnCache->cache('svnDisplayFile'.$project.$path.$previos,$this->oldfile);
								
							}
							
							$diff = $model->sideDiff($this->oldfile,$this->file);
							$this->orig = $diff['old'];
							$this->new = $diff['new'];
	
							$this->comb = $model->PHPDiff(htmlentities($this->oldfile),htmlentities($this->file));
							
							$tpl = 'diff';
							break;
						
					}
					
				}else{
					JError::raiseError( 403, JText::_( 'Not Authorized' . $action ));
					return;
				}
				
		
				
				
				
			}else{
				JError::raiseError( 404, JText::_( 'SVN Not Found' ).$this->project->svn_url);
				return;
			}
		}else{
			JError::raiseError( 403, JText::_( 'Not Authorized to Access SVN' ));
			return;
		}

        parent::display($tpl);
    }
	
	function svnLogParse($comment)
	{
		$model =& JhtraqHelper::getModel('svn');
		return $model->svnLogParse($comment,$this->project);
	}

}
?>